﻿@using RuoVea.ExUtil;

@using Microsoft.Extensions.Options

@{
    ViewData["Title"] = "作业";
    string JobRootUrl = "";
    Layout = "";
}
<!DOCTYPE html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewData["Title"] - MES管理系统</title>
    <link href="/elementui/index.css" rel="stylesheet" />
    <link href="/elementui/index.extend.css?v=22" rel="stylesheet" />
    <style scoped>


    </style>
</head>
<body>

    <el-dialog :title="dialogtitle||i18n.t('tips')" :visible="isVisible" :width="dialogwidth" :top="dialogtop" :close-on-click-modal="true" :show-close="true" :before-close="beforeClose" :close="onClose">
        <iframe ref="iframe" :src="url" width="100%" :style="dialogstyle" style="border:none;" class="iframe" frameborder="0" scrolling="yes" name="iframe" id="iframe" seamless>{{i18n.t('browserversionlow')}}</iframe>
        <div slot="footer" class="dialog-footer">
            <el-button v-on:click="onClose" size="small" v-if="showCancel">{{i18n.t('cancel')}}</el-button><el-button type="primary" v-on:click="onSubmit" size="small" v-if="showSubmit">{{i18n.t('determine')}}</el-button>
        </div>
    </el-dialog>
    <div id="main" v-cloak>
        @*         <el-extend-header
        logo-src="/img/logo.png"
        logo-title="订单管理系统"
        :menu-data="menuData"
        :active-index="activeIndex"
        user-name="百里东君"
        user-avatar=""
        login-out-txt="">
        </el-extend-header> *@
        <el-form :model="form" :rules="rules" ref="formRef">
            <el-form-item v-for="(item, index) in formJson"
                          :key="index"
                          :label="item.label"
                          :prop="item.prop">
                <!-- 根据 type 动态渲染不同的表单输入组件 -->
                <template v-if="item.type === 'input'">
                    <el-input v-model="form[item.prop]"></el-input>
                </template>
                <template v-if="item.type === 'select'">
                    <el-select v-model="form[item.prop]" placeholder="请选择">
                        <el-option v-for="option in item.options"
                                   :key="option.value"
                                   :label="option.label"
                                   :value="option.value"></el-option>
                    </el-select>
                </template>
                <!-- 根据需要添加更多的表单组件类型 -->
            </el-form-item>
        </el-form>
        <el-main>
            <el-row type="flex" align="center">
                <el-col :span="18">
                    <el-form :inline="true" :model="tableQuery.filter">
                        <el-form-item prop="jobName" label="任务名称">
                            <el-input v-model="tableQuery.filter.jobName" placeholder="任务名称" size="mini"></el-input>
                        </el-form-item>
                        <el-form-item prop="jobName" label="分组名称">
                            @* <el-input v-model="tableQuery.filter.jobGroup" placeholder="分组名称" size="mini"></el-input> *@
                            <el-select v-model="tableQuery.filter.jobGroup" clearable placeholder="请选择" size="mini">
                                <el-option key="default" label="默认" value="default"></el-option>
                                <el-option key="dev" label="开发区" value="dev"></el-option>
                                <el-option key="test" label="测试区" value="test"></el-option>
                                <el-option key="verify" label="验证区" value="verify"></el-option>
                                <el-option key="official" label="正式区" value="official"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="jobName" label="启用/禁用">
                            <el-select v-model="tableQuery.filter.status" clearable placeholder="请选择" size="mini">
                                <el-option key="0" label="启用" value="0"></el-option>
                                <el-option key="1" label="禁用" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="jobName">
                            <el-button type="primary" size="mini" v-on:click="OnLoadJobs">查询</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="5" style="text-align: right;">
                    <el-button type="primary" size="mini" icon="el-icon-plus" @@click="addDialogForm">新建任务</el-button>
                    <el-button type="default" size="mini" icon="el-icon-refresh" v-on:click="OnLoadJobs">刷新数据</el-button>
                </el-col>
            </el-row>
            <el-row type="flex" align="center">
                <el-col :span="24" style="padding: 10px 0 0 0;">
                    <el-table :data="tableData"
                              border stripe style="width: 100%;" size="small"
                              v-loading="loading">
                        <el-table-column v-for="(column, index) in columns"
                                         :key="index"
                                         :prop="column.prop"
                                         :label="column.label"
                                         :width="column.width"></el-table-column>

                        <el-table-column width="260" label="操作" fixed="right">
                            <template slot-scope="scope">
                                <el-tooltip content="查看记录" placement="bottom">
                                    <el-button type="default" size="mini" icon="el-icon-view" v-on:click="logDialog(scope.$index, scope.row)"></el-button>
                                </el-tooltip>
                                <el-tooltip content="修改记录" placement="bottom">
                                    <el-button type="primary" size="mini" icon="el-icon-edit" v-on:click="editDialogForm(scope.$index, scope.row)"></el-button>
                                </el-tooltip>
                                <el-tooltip content="删除记录" placement="bottom">
                                    <el-button type="danger" size="mini" icon="el-icon-delete-solid" v-on:click="handleDelete(scope.$index, scope.row)"></el-button>
                                </el-tooltip>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination @@size-change="handleSizeChange" @@current-change="handleCurrentChange" :current-page="tableQuery.pageNo" :page-sizes="[10, 20, 30, 40, 50]" :page-size="tableQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="tableQuery.totalRows"></el-pagination>
                </el-col>
            </el-row>
        </el-main>
        
        <el-dialog title="计划任务" :visible.sync="dialogFormVisible">
            <el-form :model="dialogForm" :rules="dialogRules" ref="dialogForm" size="small" label-width="8em">
                <el-row>
                    <el-col :span="12">
                        <el-form-item prop="jobName" label="任务名称">
                            <el-input v-model="dialogForm.jobName" autocomplete="off" placeholder="请输入内容"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="jobGroup" label="任务分组">
                            <el-select v-model="dialogForm.jobGroup" placeholder="请选择任务分组">
                                <el-option key="default" label="默认" value="default"></el-option>
                                <el-option key="dev" label="开发区" value="dev"></el-option>
                                <el-option key="test" label="测试区" value="test"></el-option>
                                <el-option key="verify" label="验证区" value="verify"></el-option>
                                <el-option key="official" label="正式区" value="official"></el-option>
                            </el-select>
                            @* <el-input v-model="dialogForm.jobGroup" placeholder="请输入内容" autocomplete="off"></el-input> *@
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="beginTime" label="开始日期">
                            <el-date-picker type="datetime" placeholder="开始日期" v-model="dialogForm.beginTime" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="endTime" label="结束时间">
                            <el-date-picker type="datetime" placeholder="结束时间" v-model="dialogForm.endTime" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>



                    <el-col :span="12" v-if="dialogForm.triggerType==1">
                        <el-form-item prop="cron" label="Cron">
                            <el-input v-model="dialogForm.cron" placeholder="请输入内容" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6" v-if="dialogForm.triggerType==0">
                        <el-form-item prop="intervalSecond" label="执行间隔/秒" label-width="8em">
                            <el-input v-model.number="dialogForm.intervalSecond" placeholder="执行间隔时间，单位秒（如果有Cron，则IntervalSecond失效）" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" v-if="dialogForm.triggerType==0">
                        <el-form-item prop="runTimes" label="执行次数" label-width="6em">
                            <el-input v-model.number="dialogForm.runTimes" placeholder="执行次数(默认无限循环)" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="12" v-if="dialogForm.runType==0">
                        <el-form-item prop="assemblyName" label="执行程序集"> <el-input v-model="dialogForm.assemblyName" placeholder="请输入执行程序集" autocomplete="off"></el-input> </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="dialogForm.runType==0">
                        <el-form-item prop="className" label="任务所在类">
                            <el-input v-model="dialogForm.className" placeholder="请输入内容" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="18" v-if="dialogForm.runType>0">
                        <el-form-item prop="requestUrl" label="请求url">
                            <el-input v-model="dialogForm.requestUrl" placeholder="请输入内容" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18" v-if="dialogForm.runType>0">
                        <el-form-item prop="headers" label="Headers">
                            <el-input type="textarea" :rows="1" v-model="dialogForm.headers" placeholder="Headers(可以包含如：Authorization授权认证) 格式：{'Authorization':'userpassword..'}" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18" v-if="dialogForm.runType==2||dialogForm.runType==3">
                        <el-form-item prop="parameters" label="Parameters"> <el-input type="textarea" :rows="1" v-model="dialogForm.parameters" placeholder="请求参数（Post，Put请求用）{' Authorization':'userpassword..'}" autocomplete="off"></el-input> </el-form-item>
                    </el-col>

                    <el-col :span="18">
                        <el-form-item prop="mailMsgType" label="邮件通知">
                            <el-select v-model="dialogForm.mailMsgType" placeholder="请选择邮件通知类型">
                                <el-option label="不通知" :value="parseInt(0)"></el-option>
                                <el-option label="错误时通知" :value="parseInt(1)"></el-option>
                                <el-option label="成功时通知" :value="parseInt(2)"></el-option>
                                <el-option label="所有情况通知" :value="parseInt(3)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18" v-if="dialogForm.mailMsgType>0">
                        <el-form-item prop="sendMailers" label="通知邮箱">
                            <el-input type="textarea" :rows="1" v-model="dialogForm.sendMailers" placeholder="请输入邮箱内容,多个邮箱请使用逗号隔开" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18">
                        <el-form-item prop="horkMsgType" label="机器人通知">
                            <el-select v-model="dialogForm.horkMsgType" placeholder="请选择通知类型">
                                <el-option label="不通知" :value="parseInt(0)"></el-option>
                                <el-option label="错误时通知" :value="parseInt(1)"></el-option>
                                <el-option label="成功时通知" :value="parseInt(2)"></el-option>
                                <el-option label="所有情况通知" :value="parseInt(3)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18" v-if="dialogForm.horkMsgType>0">
                        <el-form-item prop="horkUrl" label="通知地址">
                            <el-input v-model="dialogForm.horkUrl" placeholder="请输入机器人地址" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="18">
                        <el-form-item prop="description" label="任务描述">
                            <el-input type="textarea" :rows="2" v-model="dialogForm.description"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @@click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @@click="submitForm">确 定</el-button>
            </div>

        </el-dialog>

    </div>

    <script type="text/javascript" src="/vue/vue.js?v=2.0"></script>
    <script type="text/javascript" src="/vue/vue-i18n.min.js?v=2.0"></script>
    <script type="text/javascript" src="/elementui/index.js"></script>
    <script type="text/javascript" src="/elementui/index.extend.js"></script>
    <script type="text/javascript" src="/vue/axios.min.js"></script>
    <script>
        Vue.component('ElExtendBody', {
            props: {
                logoSrc: String,
                logoTitle: String,
                activeIndex: { type: String, },
                menuData: Array,
                userName: String,
                userAvatar: String,
                loginOutTxt: String,
                handleLogout: Function,
            },
            template: ``
        });
        var $taskVue = new Vue({
            el: "#main",
            data() {
                return {
                    tableData: [
                        {
                            date: '2016-05-03',
                            name: 'Tom',
                            address: 'No. 189, Grove St, Los Angeles',
                        }, {
                            date: '2016-05-02',
                            name: 'Tom2',
                            address: 'No. 189, Grove St, Los Angeles2',
                        },
                    ],
                    columns: [
                        {
                            prop: "date",
                            label: "日期",
                            width: 180
                        },
                        {
                            prop: "name",
                            label: "姓名",
                            width: 180
                        },
                        {
                            prop: "address",
                            label: "地址"
                        },
                        // {
                        //     prop: "action",
                        //     label: "操作",
                        //     width: 200,
                        //     type: "template",
                        //     template: `<el-button type="primary" size="small" @@click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        //      <el-button type="danger" size="small" @@click="handleDelete(scope.$index, scope.row)">删除</el-button>`
                        // }
                    ],
                    loading: false,


                    filter: [
                        {
                            type: 'input', // 表单项类型
                            label: '姓名', // 标签文本
                            prop: 'name',  // 表单项的属性名，对应 model 中的字段
                            rules: [{ required: true, message: '姓名不能为空', trigger: 'blur' }] // 校验规则
                        },
                        {
                            type: 'select',
                            label: '性别',
                            prop: 'gender',
                            options: [ // 选择项列表
                                { label: '男', value: 'male' },
                                { label: '女', value: 'female' }
                            ],
                            rules: [{ required: true, message: '性别不能为空' }]
                        },
                        // ...其他表单项配置
                    ],
                    tableQuery: {
                        pageNo: 1,
                        pageSize: 10,
                        totalRows: 0,
                        filter: {
                            jobName: '',
                            jobGroup: "",
                            status: ""
                        }
                    },

                    dialogFormVisible: false,
                    dialogForm: { id: 0, schedName: '', jobName: '', jobGroup: 'default', beginTime: Date.parse('@(DateTime.Now.ToDateTimeString())'), endTime: Date.parse('@(DateTime.MaxValue.ToDateTimeString())'), cron: '', runTimes: 0, intervalSecond: 0, scheduleType: 0, triggerType: 0, runType: 1, triggerState: 0, requestUrl: '', headers: '', parameters: '', description: '', mailMsgType: 0, sendMailers: "", horkMsgType: 0, horkUrl: "", assemblyName: '', className: '' },
                    dialogInitForm: { id: 0, schedName: '', jobName: '', jobGroup: 'default', beginTime: Date.parse('@(DateTime.Now.ToDateTimeString())'), endTime: Date.parse('@(DateTime.MaxValue.ToDateTimeString())'), cron: '', runTimes: 0, intervalSecond: 0, scheduleType: 0, triggerType: 0, runType: 1, triggerState: 0, requestUrl: '', headers: '', parameters: '', description: '', mailMsgType: 0, sendMailers: "", horkMsgType: 0, horkUrl: "", assemblyName: '', className: '' },

                    dialogRules: {
                        jobName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }, { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }],
                        // jobGroup: [{ required: true, message: '请输入任务分组', trigger: 'blur' }, { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }],
                        beginTime: [{ required: true, message: '请输入开始日期', trigger: 'change' }],
                        endTime: [{ required: true, message: '请输入结束时间', trigger: 'change' }],
                        cron: [{ required: true, message: '请输入Cron表达式', trigger: 'blur' }],
                        intervalSecond: [{ required: true, message: '请输入执行间隔/秒', trigger: 'blur' }, { type: 'number', message: '必须为数字值' }],
                        runTimes: [{ type: 'number', message: '必须为数字值' }],
                        requestUrl: [{ required: true, message: '请输入请求url', trigger: 'blur' }],
                        // headers: [{ required: true, message: '请输入Headers', trigger: 'blur' }],
                        // parameters: [{ required: true, message: '请输入Parameters', trigger: 'blur' }],
                        sendMailers: [{ required: true, message: '请输入通知邮箱', trigger: 'blur' }],
                        horkUrl: [{ required: true, message: '请输入机器人地址', trigger: 'blur' }],
                        assemblyName: [{ required: true, message: '请输入执行程序集', trigger: 'blur' }],
                        className: [{ required: true, message: '请输入任务所在类', trigger: 'blur' }],
                    },
                }
            },
            created() {
                // setInterval(() => { this.OnLoadJobs(); }, 10000);
            },
            mounted: function () {
                // this.OnLoadJobs();
            },
            methods: {
                submitForm() {
                    this.$refs.formRef.validate((valid) => {
                        if (valid) {
                            alert('表单验证成功!');
                            // 表单提交逻辑
                        } else {
                            console.error('表单验证失败!');
                            return false;
                        }
                    });
                },
                handleEdit(index, row) {
                    // 编辑操作的逻辑
                },
                handleDelete(index, row) {
                    // 删除操作的逻辑
                },
                OnLoadJobs() {
                    var that = this;
                    that.loading = true;
                    axios.post("@("/jobList")", that.tableQuery)
                        .then(function (res) {
                            that.tableData = res.data.data.rows;
                            that.tableQuery.totalRows = res.data.data.totalRows;
                            that.loading = false;
                        }).catch(function (res) { that.loading = false; });
                },
                handleSizeChange(val) { var that = this; that.tableQuery.pageNo = 1; that.tableQuery.pageSize = val; that.OnLoadJobs(); },
                handleCurrentChange(val) { var that = this; that.tableQuery.pageNo = val; that.OnLoadJobs(); },

                addDialogForm() {
                    var that = this;
                    that.dialogFormVisible = true; that.dialogForm = null;
                    that.dialogForm = that.dialogInitForm;
                },
                editDialogForm(index, row) {
                    var that = this;
                    that.dialogFormVisible = true;
                    that.dialogForm = that.dialogInitForm;
                    axios("@("/" + JobRootUrl + "/getjob/")" + row.id).then(function (res) { if (res.data.code == 200) { that.dialogForm = res.data.data; } });
                },
                logDialog(index, row) { var that = this; window.location.href = "@("/" + JobRootUrl + "/log?id=")" + row.id; },
                submitForm() {
                    var that = this;
                    that.$refs["dialogForm"].validate((valid) => {
                        if (valid) {
                            axios.post("@("/" + JobRootUrl + "/jobsave")", that.dialogForm).then(function (res) {
                                if (res.data.code == 200) {
                                    ELEMENT.MessageBox.alert(res.data.message, "系统信息", { type: 'success', callback: () => { that.OnLoadJobs(); that.dialogForm = that.dialogInitForm; that.dialogFormVisible = false; } });
                                } else {
                                    ELEMENT.MessageBox.alert(res.data.message, "系统信息", { type: 'info', callback: () => { } });
                                }
                            });
                        }
                    });
                },
                handleStart(id) {
                    var that = this;
                    this.$confirm('确定开启此任务?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                        axios("@("/" + JobRootUrl + "/start/")" + id).then(function (res) {
                            if (res.data.code == 200) {
                                that.$message({ message: res.data.message, center: true, type: 'success' });
                                that.OnLoadJobs();
                            } else {
                                that.$message({ message: res.data.message, center: true, type: 'error' });
                            }
                        });
                    });
                },
                handleStop(id) {
                    var that = this;
                    this.$confirm('确定暂停此任务?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                        axios("@("/" + JobRootUrl + "/stop/")" + id).then(function (res) {
                            if (res.data.code == 200) {
                                that.$message({ message: res.data.message, center: true, type: 'success' });
                                that.OnLoadJobs();
                            } else {
                                that.$message({ message: res.data.message, center: true, type: 'error' });
                            }
                        });
                    });

                },
                handleRun(id) {
                    var that = this;
                    this.$confirm('确定运行此任务?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                        axios("@("/" + JobRootUrl + "/run/")" + id).then(function (res) {
                            if (res.data.code == 200) {
                                that.$message({ message: res.data.message, center: true, type: 'success' });
                                that.OnLoadJobs();
                            } else {
                                that.$message({ message: res.data.message, center: true, type: 'error' });
                            }
                        });
                    });
                },
                handleDelete(index, row) {
                    var that = this;
                    this.$confirm('确定删除此任务?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                        axios("@("/" + JobRootUrl + "/remove/")" + row.id).then(function (res) {
                            if (res.data.code == 200) {
                                that.$message({ message: res.data.message, center: true, type: 'success' });
                                rows.splice(index, 1);
                                that.OnLoadJobs();
                            } else {
                                that.$message({ message: res.data.message, center: true, type: 'error' });
                            }
                        });
                    });
                }
            },
            filters: {
                scheduleTypeTitle(data) {
                    return data.scheduleTypeText + ":" + data.runTypeText;
                }
            }
        });
    </script>
</body>
</>
